package com.firegod.group;

import com.alibaba.fastjson.JSONObject;
import com.firegod.commonutils.vo.ResultVo;
import com.firegod.group.service.CommentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;

@RestController
@RequestMapping("/group")
@CrossOrigin
@Api(tags = "圈子管理")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/createComment")
    public ResultVo createComment(@NotNull @RequestBody JSONObject jsonObject) {
        String topicId = jsonObject.getString("topicId");
        String topicMessage = jsonObject.getString("topicMessage");
        return ResultVo.ok().data("commentId", commentService.createComment(topicId, topicMessage));
    }

    @PostMapping("/createReply")
    public ResultVo createReply(@RequestBody JSONObject jsonObject) {
        String replyTopicMessage = jsonObject.getString("replyTopicMessage");
        String replyToUserId = jsonObject.getString("replyToUserId");
        String replyToUserName = jsonObject.getString("replyToUserName");
        String topicMessageId = jsonObject.getString("replyTopicMessageId");
        String topicId = jsonObject.getString("topicId");
        return ResultVo.ok().data("commentId",
                commentService.createReply(replyTopicMessage, replyToUserId, replyToUserName, topicMessageId, topicId));
    }

    @PostMapping("/deleteTopicMessage/{topicMessageId}")
    public ResultVo deleteComment(@NotNull @PathVariable String topicMessageId) {
        commentService.deleteComment(topicMessageId);
        return ResultVo.ok();
    }

    @GetMapping("/comments/{topicId}")
    public ResultVo getComments(@NotNull @PathVariable String topicId) {
        var topicComments = commentService.getTopicComment(topicId);
        var topicReplyComments = commentService.getReplyComment(topicId);
        return ResultVo.ok().data(new HashMap<>() {{
            put("topicMessageList", topicComments);
            put("topicMessageReplyList", topicReplyComments);
        }});
    }
}
